package com.hfqby.camera.common.utils;

/**
 * 判断工具类
 *
 * @author qiu bo yang
 * @date 2023-05-29
 */
public class AssertUtils<T> {

    /**
     * 判断data是否为null,为null抛出异常
     *
     * @param data     数据
     * @param errorMsg 错误信息
     * @return T data
     * @throws IllegalArgumentException 参数格式错误
     */
    public static <T> T notNull(T data, String errorMsg) {
        if (data == null) {
            throw new IllegalArgumentException(errorMsg);
        }
        if (data instanceof String) {
            String dataStr = (String) data;
            if (dataStr == null || dataStr.length() == 0) {
                throw new IllegalArgumentException(errorMsg);
            }
        }
        return data;
    }

    /**
     * 判断data是否为null,为null抛出异常
     *
     * @param data 数据
     * @param e    错误异常
     * @return T data
     * @throws RuntimeException 参数格式错误
     */
    public static <T> T notNull(T data, RuntimeException e) {
        if (data == null) {
            throw e;
        }
        if (data instanceof String) {
            String dataStr = (String) data;
            if (dataStr == null || dataStr.length() == 0) {
                throw e;
            }
        }
        return data;
    }

    /**
     * 判断表达式是否返回true
     *
     * @param expression 表达
     * @param errorMsg   错误信息
     * @throws IllegalArgumentException 参数格式错误
     */
    public static void isTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    /**
     * 判断表达式是否返回false
     *
     * @param expression 表达
     * @param errorMsg   错误信息
     */
    public static void isFalse(boolean expression, String errorMsg) {
        isTrue(!expression, errorMsg);
    }

    /**
     * 抛出非法数据异常
     *
     * @param errorMsg 错误信息
     * @throws IllegalArgumentException 参数格式错误
     */
    public static void error(String errorMsg) {
        throw new IllegalArgumentException(errorMsg);
    }

    /**
     * 抛出非法数据异常
     *
     * @param e 错误异常对象
     * @throws RuntimeException 运行时异常
     */
    public static void error(RuntimeException e) {
        throw e;
    }

}
